<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Attr;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class AttrController extends Controller
{
    //get  admin/attr   全部分类列表
    public function index(){
        
    }



    //post  admin/attr  获取要添加的数据并存储到数据库
    public function store(){
        $input = Input::except('_token');
//        dd($input);
        //验证
        $rules = [
            'aname'=>'required'
        ];
        $message=[
            'aname.required'=>'类型属性名称不能为空'
        ];

        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $re = Attr::create($input);
            if($re){
                return redirect('admin/attr/'.$input['type_tid']);
            }else{
                return back()->with('errors','数据填入失败');
            }
        }else{
            return back()->withErrors($validator);
        }

    }

    //get. admin/attr/create   添加页面的显示及数据分配
    public function create(){
        
        return view('admin.attr.add');
    }

    //get admin/attr/{attr}
    public function show($tid){
        $attr_datas = Attr::where('type_tid',$tid)->get();
        
        return view('admin.attr.index',compact('attr_datas','tid'));
    }

    //delete admin/attr/{attr} 
    public function destroy($aid){
        $re = Attr::where('aid',$aid)->delete();
        if($re){
            $data=[
               'status'=>1,
               'msg'=>'类型属性删除成功'
            ];
        }else{
            $data=[
                'status'=>0,
                'msg'=>'类型属性删除失败'
            ];
        }
        return $data;
    }

    //put admin/attr/{attr} 
    public function update($aid){
        $input = Input::except('_token','_method');
//        dd($input);
        $re = Attr::where('aid',$aid)->update($input);
        if($re){
            return redirect('admin/attr/'.$input['type_tid']);
        }else{
            return back()->with('msg','类型属性更改失败');
        }
    }

    //get admin/attr/{attr}/edit
    public function edit($aid){
        $attr_data = Attr::find($aid);
        
        return view('admin.attr.edit',compact('attr_data'));
    }
}
